package com.jerry.constellation.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jerry.constellation.Adapter.LuckyDetialsAdapter;
import com.jerry.constellation.Adapter.LuckyDetialsItemBean;
import com.jerry.constellation.R;
import com.jerry.constellation.Utils.LoadDataDetialTask;
import com.jerry.constellation.Utils.URLContent;
import com.jerry.constellation.bean.LuckyDetialsBean;

import java.util.ArrayList;
import java.util.List;


public class LuckyDetailsActivity extends AppCompatActivity implements LoadDataDetialTask.OnGetNetDataListener {

    private ListView luckyLv;
    private TextView name;
    private TextView content;
    private ImageView back;
    private String namegetfrombefore;
    private List<LuckyDetialsItemBean> mdata;
    private LuckyDetialsAdapter luckyDetialsAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lucky_details);
        namegetfrombefore = getIntent().getStringExtra("name");
        //ToastUtils.showImageToas(LuckyDetailsActivity.this,name);
        //获取网址
        String url = URLContent.getLuckyURL(namegetfrombefore);
        initView();
        mdata = new ArrayList<>();
        //获取网络请求
        LoadDataDetialTask task = new LoadDataDetialTask(this, this, true);
        task.execute(url);
    }

    @Override
    public void onSuccess(String json) {
        if (!TextUtils.isEmpty(json)) {
            //数据的解析
            LuckyDetialsBean bean = new Gson().fromJson(json, LuckyDetialsBean.class);
            //为了显示在listview上，再将数据进行整理
            addDataToList(bean);
            //设置适配器
            luckyDetialsAdapter = new LuckyDetialsAdapter(this, mdata);
            luckyLv.setAdapter(luckyDetialsAdapter);
            //更新适配器

        }
    }

    /*
     * TODO:整理数据到集合中
     * */
    private void addDataToList(LuckyDetialsBean bean) {
        LuckyDetialsItemBean ldib1 = new LuckyDetialsItemBean("综合运势", bean.getMima().getText().get(0), Color.BLUE);
        LuckyDetialsItemBean ldib2 = new LuckyDetialsItemBean("爱情运势", bean.getLove().get(0), Color.LTGRAY);
        LuckyDetialsItemBean ldib3 = new LuckyDetialsItemBean("事业学业", bean.getCareer().get(0), Color.GREEN);
        LuckyDetialsItemBean ldib4 = new LuckyDetialsItemBean("健康运势", bean.getHealth().get(0), Color.RED);
        LuckyDetialsItemBean ldib5 = new LuckyDetialsItemBean("财富运势", bean.getFinance().get(0), Color.GRAY);
        //添加进入集合
        mdata.add(ldib1);
        mdata.add(ldib2);
        mdata.add(ldib3);
        mdata.add(ldib4);
        mdata.add(ldib5);



    }

    private void initView() {
        luckyLv = findViewById(R.id.lucky_detials_Lv);
        name = findViewById(R.id.title_tv);
        content = findViewById(R.id.lucky_detials_Lv_item_content);
        back = findViewById(R.id.title_iv_back);

        name.setText(namegetfrombefore);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }


}